package com.taomu.app.zongchouhuli.moudle.home.contract.impl;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResOrderInfoEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductDetailInfoEntity;
import com.taomu.app.zongchouhuli.http.res.ResSubmitOrderEntity;
import com.taomu.app.zongchouhuli.moudle.home.contract.HomeOrderContract;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class HomeOrderPresenter implements HomeOrderContract.Presenter {

    private HomeOrderContract.View mContractView;

    private CompositeDisposable disposable;

    public HomeOrderPresenter(HomeOrderContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getOrderInfo(String productid, String num, String incometype,String distribution) {
        Request.getIntence().getOrderInfo(StorageHelper.getToken(),productid,num,incometype,distribution)
                .flatMap(new Function<String, Observable<BaseChangeResponse<ResSubmitOrderEntity>>>() {
                    @Override
                    public  Observable<BaseChangeResponse<ResSubmitOrderEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResSubmitOrderEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResSubmitOrderEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResSubmitOrderEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResSubmitOrderEntity> data) {
                        if(mContractView != null){
                            mContractView.setOrderInfo(data.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Log.d("wqdqwdwqdwqdwq","onError:"+e.getMessage());
                    }
                });
    }

    @Override
    public void setsubmit(String order_id, String password, String product_id,String distribution) {
        Request.getIntence().getPayOrder(StorageHelper.getToken(),order_id,password,product_id,distribution)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String baseResponse) {
                        if(mContractView != null){
                            mContractView.setSubmitResults(baseResponse);
                        }
                    }
                });
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {

    }
}
